var createError = require('http-errors');
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var registRouter = require('./routes/regist');
var signupRouter = require('./routes/signup');
var postArticleRouter = require('./routes/post_article');
var getArticleRouter = require('./routes/get_article');
var getDetailRouter = require('./routes/get_detail');
var supportRouter = require('./routes/support');
var commentRouter = require('./routes/comment');
var queryRouter = require('./routes/query');
var myArticleRouter = require('./routes/my_article');
var getRawUserRouter = require('./routes/get_rawuser');
var getUserRouter = require('./routes/get_user');
var modifyUserRouter = require('./routes/modify_user');
var getSupportRouter = require('./routes/get_support');
var getCommentRouter = require('./routes/get_comment');
var mySupportRouter = require('./routes/my_support');
var myCommentRouter = require('./routes/my_comment');
var getSpecifyArticleRouter = require('./routes/get_spec_article');


var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/regist',registRouter);
app.use('/signup',signupRouter);
app.use('/post_article',postArticleRouter);
app.use('/get_article',getArticleRouter);
app.use('/get_detail',getDetailRouter);
app.use('/support',supportRouter);
app.use('/comment',commentRouter);
app.use('/query',queryRouter);
app.use('/my_article',myArticleRouter);
app.use('/my_support',mySupportRouter);
app.use('/get_rawuser',getRawUserRouter);
app.use('/my_comment',myCommentRouter);
app.use('/get_user',getUserRouter);
app.use('/modify_user',modifyUserRouter);
app.use('/get_support',getSupportRouter);
app.use('/get_comment',getCommentRouter);
app.use('/get_spec_article',getSpecifyArticleRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
